package com.liuche.homechat.common.websocket.service.adapter;

import com.liuche.homechat.common.user.domain.entity.User;
import com.liuche.homechat.common.websocket.domaiin.enums.UserPowerEnum;
import com.liuche.homechat.common.websocket.domaiin.enums.WSRespTypeEnum;
import com.liuche.homechat.common.websocket.domaiin.vo.resp.WSBaseResp;
import com.liuche.homechat.common.websocket.domaiin.vo.resp.WSLoginSuccess;
import com.liuche.homechat.common.websocket.domaiin.vo.resp.WSLoginUrl;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;

/**
 * @author：liuche
 * @Date：2024年05月26日 20:57
 * @Description：
 */
public class WebSocketAdapter {
    /**
     * 构建返回二维码信息的返回值
     * @return
     */
    public static WSBaseResp<WSLoginUrl> getResp(WxMpQrCodeTicket codeTicket) {
        WSBaseResp<WSLoginUrl> resp = new WSBaseResp<>();
        resp.setType(WSRespTypeEnum.LOGIN_URL.getType());
        resp.setData(WSLoginUrl.builder()
                .loginUrl(codeTicket.getUrl())
                .build());
        return resp;
    }

    /**
     * 得到登录消息的响应
     * @return
     */
    public static WSBaseResp<WSLoginSuccess> getLoginResp(User user, String token) {
        WSBaseResp<WSLoginSuccess> resp = new WSBaseResp<>();
        resp.setType(WSRespTypeEnum.LOGIN_SUCCESS.getType());
        resp.setData(WSLoginSuccess.builder()
                .uid(user.getId())
                .token(token)
                .name(user.getName())
                .power(UserPowerEnum.COMMON_USER.getCode())
                .avatar(user.getAvatar())
                .build());
        return resp;
    }

    /**
     * 在界面提示用户前往微信进行授权登录
     * @return
     */
    public static WSBaseResp<String> getPromptUserAuthorizeResp() {
        WSBaseResp<String> resp = new WSBaseResp<>();
        resp.setType(WSRespTypeEnum.LOGIN_SCAN_SUCCESS.getType());
        resp.setData("请在微信上授权进行登录！");

        return resp;
    }

    public static WSBaseResp<String> buildInvalidateTokenResp() {
        WSBaseResp<String> resp = new WSBaseResp<>();
        resp.setType(WSRespTypeEnum.INVALIDATE_TOKEN.getType());
        resp.setData("登录信息失效！");

        return resp;
    }

    public static WSBaseResp<Long> getUserOnlineNoticeInfo(User user) {
        WSBaseResp<Long> resp = new WSBaseResp<>();

        resp.setType(WSRespTypeEnum.ONLINE_OFFLINE_NOTIFY.getType());
        resp.setData(user.getId());

        return resp;
    }
}
